package demo3;

public class Text01 {

    //大数可以使用_分隔符
    private static long count = 10_0000_0000l;

    public static void main(String[] args) {
        //串行
        serial();
        //并行
        concurrency();
    }

    private static void concurrency() {
        //记录开始时间
        long begin = System.currentTimeMillis();

        //分别定义两个线程，各自执行累加操作
        //第一个线程
        Thread thread1 = new Thread(() -> {
           long a = 0l;
            for (long i = 0; i < count; i++) {
                a++;
            }
        });
        //启动thread1线程
        thread1.start();

        //第二个线程
        Thread thread2 = new Thread(() -> {
            long b = 0l;
            for (long i = 0; i < count; i++) {
                b++;
            }
        });
        //启动thread2线程
        thread2.start();

        //等待thread1和thread2执行完成
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //记录结束时间
        long end  = System.currentTimeMillis();
        System.out.println("并行执行耗时：" + (end - begin));
    }


    private static void serial() {
        // 记录开始时间
        long begin = System.currentTimeMillis();
        long a = 0l;
        for (int i = 0; i < count; i++) {
            a++;
        }
        long b = 0l;
        for (int i = 0; i < count; i++) {
            b++;
        }
        // 记录结束时间
        long end = System.currentTimeMillis();
        System.out.println("串行时间耗时：" + (end - begin));
    }
}
